import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class ArrayListTest {
    private static List<String> list = new ArrayList<>();

    public static void main(String[] args) {
        list.add("1");
        list.add("2");
        list.add("3");
        final Iterator<String> iterator = list.iterator();
        //初始化一个线程数为10的线程池
        ExecutorService service = Executors.newFixedThreadPool(10);
        //10个读线程
        for (int i=0;i<10;i++){
            service.execute(new Runnable() {
                @Override
                public void run() {
                    while (iterator.hasNext()){
                        System.out.println(iterator.next());
                    }
                }
            });
        }
        //10个写线程
        for (int i=0;i<10;i++){
            service.execute(new Runnable() {
                @Override
                public void run() {
                    list.add("愚公");
                }
            });
        }
    }
}
